package api

import (
	"blizzard/app/model"
	"blizzard/app/service"
	"blizzard/app/utils/code"
	"blizzard/app/utils/response"
	"github.com/gogf/gf/net/ghttp"
)

var User = new(apiUser)

type apiUser struct {}

func (a *apiUser) Register(r *ghttp.Request) {
	var (
		data *model.UserRegisterParam
		err error
	)
	if err = r.Parse(&data); err != nil {
		response.JsonExit(r, code.ParamError, err.Error())
	}
	id := r.GetInt("app_id")
	if err = service.User.Register(r, id, data); err != nil {
		response.JsonExit(r, code.UserRegisterError, err.Error())
	}
	response.Json(r, code.Success, code.SuccessMsg)
}

func (a *apiUser) Recharge(r *ghttp.Request) {
	var (
		data *model.UserRechargeParam
		err error
	)
	if err = r.Parse(&data); err != nil {
		response.JsonExit(r, code.ParamError, err.Error())
	}
	id := r.GetInt("app_id")
	err, mk := service.User.Recharge(id, data)
	if err != nil {
		response.JsonExit(r, code.ServerError, err.Error())
	}
	response.Json(r, code.Success, code.SuccessMsg, mk)
}

func (a *apiUser) PassChange(r *ghttp.Request) {
	var (
		data *model.UserPassChangeParam
		err error
	)
	if err = r.Parse(&data); err != nil {
		response.JsonExit(r, code.ParamError, err.Error())
	}
	id := r.GetInt("app_id")
	change, err := service.User.PassChange(id, data)
	if err != nil {
		response.JsonExit(r, code.ServerError, err.Error())
	}
	response.Json(r, code.Success, code.SuccessMsg, change)
}

func (a *apiUser) Login(r *ghttp.Request) {
	var (
		data *model.UserLoginParam
		err error
	)
	if err = r.Parse(&data); err != nil {
		response.JsonExit(r, code.ParamError, err.Error())
	}
	id := r.GetInt("app_id")
	user, err := service.User.Login(r, id, data)
	if err != nil {
		response.JsonExit(r, code.ServerError, err.Error())
	}
	response.Json(r, code.Success, code.SuccessMsg, user)
}

func (a *apiUser) Logout(r *ghttp.Request) {
	var (
		data *model.UserLogoutParam
		err error
	)
	if err = r.Parse(&data); err != nil {
		response.JsonExit(r, code.ParamError, err.Error())
	}
	_ = r.GetInt("app_id")
	err = service.User.Logout(data)
	if err != nil {
		response.JsonExit(r, code.ServerError, err.Error())
	}
	response.Json(r, code.Success, "退出" + code.SuccessMsg)
}

func (a *apiUser) UnBind(r *ghttp.Request) {
	var (
		data *model.UserUnbindParam
		err error
	)
	if err = r.Parse(&data); err != nil {
		response.JsonExit(r, code.ParamError, err.Error())
	}
	id := r.GetInt("app_id")
	bind, err := service.User.UnBind(id, data)
	if err != nil {
		response.JsonExit(r, code.ServerError, err.Error())
	}
	response.Json(r, code.Success, "解绑" + code.SuccessMsg, bind)
}

func (a *apiUser) Trial(r *ghttp.Request) {
	var (
		data *model.UserTrialParam
		trial interface{}
		err error
	)
	id := r.GetInt("app_id")
	if err = r.Parse(&data); err != nil {
		response.JsonExit(r, code.ParamError, err.Error())
	}
	trial, err = service.User.Trial(id, data)
	if err != nil {
		response.JsonExit(r, code.ServerError, err.Error())
	}
	response.Json(r, code.Success, "试用" + code.SuccessMsg, trial)
}

func (a *apiUser) Heart(r *ghttp.Request) {
	var (
		data *model.UserHeartParam
		err error
		id int
	)
	id = r.GetInt("app_id")
	if err = r.Parse(&data); err != nil {
		response.JsonExit(r, code.ParamError, err.Error())
	}
	err = service.User.Heart(id, data)
	if err != nil {
		response.JsonExit(r, code.ServerError, err.Error())
	}
	response.Json(r, code.Success, code.SuccessMsg)
}

func (a *apiUser) GetUserData(r *ghttp.Request) {
	var (
		data *model.GetUserDataParam
		err error
	)
	id := r.GetInt("app_id")
	if err = r.Parse(&data); err != nil {
		response.JsonExit(r, code.ParamError, err.Error())
	}
	userData, err := service.User.GetUserData(id, data)
	if err != nil {
		response.JsonExit(r, code.ServerError, err.Error())
	}
	response.Json(r, code.Success, code.SuccessMsg, userData)
}

func (a *apiUser) SetUserData(r *ghttp.Request) {
	var (
		data *model.SetUserDataParam
		err error
		id int
	)
	if err = r.Parse(&data); err != nil {
		response.JsonExit(r, code.ParamError, err.Error())
	}
	id = r.GetInt("app_id")
	err = service.User.SetUserData(id, data)
	if err != nil {
		response.JsonExit(r, code.ServerError, err.Error())
	}
	response.Json(r, code.Success, code.SuccessMsg)
}

func (a *apiUser) QueryCard(r *ghttp.Request) {
	var (
		data *model.QueryCardParam
		err error
		card interface{}
	)
	if err = r.Parse(&data); err != nil {
		response.JsonExit(r, code.ParamError, err.Error())
	}
	card, err = service.User.QueryCard(data)
	if err != nil {
		response.JsonExit(r, code.ServerError, err.Error())
	}
	response.Json(r, code.Success, code.SuccessMsg, card)
}